package com.polymer.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.polymer.app.controller.utils.RedisUtils;
import com.polymer.app.utils.LoggerUtil;

@Controller
public class RedisSessionController {
    private final static Logger logger = Logger.getLogger(RedisSessionController.class);
    @Resource
    private RedisUtils redisUtils;
    
    @GetMapping("/set")
    @ResponseBody
    public String set(HttpSession session) {
        System.out.println("setsession");
        LoggerUtil.info(logger, "setsession");
        setConfig(session);
        return String.valueOf("1");
    }

    @GetMapping("/get")
    @ResponseBody
    public String get(HttpSession session) {
        System.out.println("getsession");
        LoggerUtil.info(logger, "getsession");
        Config config = getConfig(session);
        return JSON.toJSONString(config) ;
    }
    
    @GetMapping("/getkey")
    @ResponseBody
    public String getkey() {
        LoggerUtil.info(logger, "getredis");
        String value =null;
        try {
            value = redisUtils.get("room1");
            LoggerUtil.info(logger, "获取redis:{0}", value);
        } catch (Exception e) {
            LoggerUtil.error(e,logger, "获取redis异常:{0}", e);
        }
        return value;
    }
    
    @GetMapping("/setkey")
    @ResponseBody
    public String setkey() {
        LoggerUtil.info(logger, "setredis");
        String value =null;
        try {
            value = redisUtils.get("room1");
            LoggerUtil.info(logger, "获取redis:{0}", value);
            redisUtils.set("room2", value);
        } catch (Exception e) {
            LoggerUtil.error(e,logger, "获取redis异常:{0}", e);
        }
        return value;
    }
    
    private Config getConfig(HttpSession session){
        Config c=new Config();
        c.setAge((String)session.getAttribute("age"));
        c.setCar((String)session.getAttribute("car"));
        c.setPhone((String)session.getAttribute("phone"));
        c.setUserId((String)session.getAttribute("userId"));
        return c;
    }
    
    private void setConfig(HttpSession session){
        session.setAttribute("age", "22");
        session.setAttribute("car", "taxi");
        session.setAttribute("phone", "18997966144");
        session.setAttribute("userId", "2088622943096971");
        LoggerUtil.info(logger, "setredis{0}",session.getId());
    }
}
class Config{
    private String userId;
    
    private String car;
    
    private String phone;
    
    private String age;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getCar() {
        return car;
    }

    public void setCar(String car) {
        this.car = car;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    
    
}